package com.example.impala.jdbc;

import com.example.impala.csv.CsvUtil;
import com.example.impala.model.ImpalaArg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author: LiuJie
 * @Date: 2019-04-22 16:20
 * @Description:
 */
@Component
public class Jdbc {
    @Autowired
    private CsvUtil csvUtil;

    public List executeJdbc(ImpalaArg arg) {
        Connection con = null;
        ResultSet rs = null;
        PreparedStatement ps = null;
        String JDBC_DRIVER = "com.cloudera.impala.jdbc41.Driver";
//        String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver";
//        String CONNECTION_URL = "jdbc:impala://172.22.6.113:21000";
        String CONNECTION_URL = arg.getConnectUrl();
        try {
            Class.forName(JDBC_DRIVER);
            if (null != arg.getPassword() && arg.getPassword().trim() != "" && null != arg.getUsername() && arg.getUsername().trim() != "") {
                con = DriverManager.getConnection(CONNECTION_URL, arg.getUsername(), arg.getPassword());
            } else {
                con = DriverManager.getConnection(CONNECTION_URL);
            }

            ps = con.prepareStatement(arg.getSql());
            rs = ps.executeQuery();
            List<Map<String, Object>> rlt = resultSetToList(rs);

            return rlt;
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e.getMessage());
        } finally {
            try {
                if (null != rs) {
                    rs.close();
                }
                if (null != ps) {
                    ps.close();
                }
                if (null != con) {
                    con.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public List<Map<String, Object>> resultSetToList(ResultSet rs) throws SQLException {
        List<Map<String, Object>> results = new ArrayList<>();
        ResultSetMetaData rsmd = rs.getMetaData();
        int colCount = rsmd.getColumnCount();
        List<String> colNameList = new ArrayList<>();
        for (int i = 0; i < colCount; i++) {
            colNameList.add(rsmd.getColumnName(i + 1));
        }
        while (rs.next()) {
            Map map = new HashMap<String, Object>();
            for (int i = 0; i < colCount; i++) {
                String key = colNameList.get(i);
                Object value = rs.getString(colNameList.get(i));
                map.put(key, value);
            }
            results.add(map);
        }
        return results;
    }

    public static void main(String[] args) {
        Jdbc jdbc = new Jdbc();
        ImpalaArg arg = new ImpalaArg();
        arg.setUsername("root");
        arg.setPassword("root");
        arg.setConnectUrl("jdbc:mysql://120.55.37.41:3306/awms?useSSL=false&serverTimezone=UTC");
        arg.setSql("SELECT * FROM td_settings_group_master");
//        List list = jdbc.executeJdbc(arg);
        System.err.println();
    }

    public List findData(ImpalaArg arg) {
        List<Map<String, Object>> rlt = executeJdbc(arg);
        Integer limit = arg.getLimit();
        if (null == limit) {
            limit = 100;
        }
        if (rlt.size() != 0) {
            csvUtil.dataPersistence(arg.toString(), rlt);
        }
        return rlt.stream().limit(limit).collect(Collectors.toList());
    }
}
